/*********************************************************************************
 *FileName:  eeprom.c
 *Author:    ChenJiehong
 *Version:   V1.0
 *Date:      2023-11-29
 ****************************************Includes***********************************/
#include "eeprom.h"
#include "bdc.h"
#include "sensor.h"

/***************************************Variables***********************************/


/***************************************Functions***********************************/

/**
 * @brief EEPROM 存储执行器参数
 * @param None
 * @retval None
 */
void EEPROM_StoreActuatorParamsconfig(EEPROM_ActuatorParams_t *ActuatorParams)
{
    ActuatorParams->Actuator_Sensor_Sum = ActuatorParams->Actuator_Sensor_Min + ActuatorParams->Actuator_Sensor_Max;
    EEPROM_Unlock();
    /* 第20页 */
    EEPROM_ErasePage(EEPROM_ACTUATOR_PARM_PAGE);
    EEPROM_ProgramPage(EEPROM_ACTUATOR_PARM_PAGE, (uint32_t*)ActuatorParams);
}

/**
 * @brief EEPROM 读取执行器参数
 * @param None
 * @retval None
 */
void EEPROM_ReadActuatorParamsconfig(EEPROM_ActuatorParams_t *ActuatorParams)
{
    uint64_t eepromCode = *((uint64_t *)(EEPROMMEM_BASE + 8 * EEPROM_ACTUATOR_PARM_PAGE));

    ActuatorParams->Actuator_Sensor_Min = (uint16_t)(eepromCode & 0xFFFF);
    eepromCode = eepromCode >> 16;
    ActuatorParams->Actuator_Sensor_Max = (uint16_t)(eepromCode & 0xFFFF);
    eepromCode = eepromCode >> 16;
    ActuatorParams->Actuator_Sensor_Sum = (uint16_t)(eepromCode & 0xFFFF);
    eepromCode = eepromCode >> 16;
    ActuatorParams->Actuator_Sensor_Cnt = (uint16_t)(eepromCode & 0xFFFF);

    if (((ActuatorParams->Actuator_Sensor_Min + ActuatorParams->Actuator_Sensor_Max) ==
         ActuatorParams->Actuator_Sensor_Sum) && (ActuatorParams->Actuator_Sensor_Cnt !=0 ) &&
        (ActuatorParams->Actuator_Sensor_Min != 0 && ActuatorParams->Actuator_Sensor_Max != 0))
    {
        Position_Para.u16Sensor_Min = ActuatorParams->Actuator_Sensor_Min + RESISTANCE_MIN_OFFSET;

        if (ActuatorParams->Actuator_Sensor_Max < ActuatorParams->Actuator_Sensor_Min)
        {
            Position_Para.u16Sensor_Max = ActuatorParams->Actuator_Sensor_Max + 0x3FFF - RESISTANCE_MAX_OFFSET;
		}
        else
        {
            Position_Para.u16Sensor_Max = ActuatorParams->Actuator_Sensor_Max - RESISTANCE_MAX_OFFSET;
        }
    }
    else
    {
        Position_Para.u16Sensor_Min = RESISTANCE_MIN_NORMAL;
        Position_Para.u16Sensor_Max = RESISTANCE_MAX_NORMAL;
		ActuatorParams->Actuator_Sensor_Cnt = 0;
    }
}

/***************************************END OF FILE*********************************/
